package com.lwc.cfdns.common;

import com.lwc.cfdns.enums.ResultCode;
import com.lwc.cfdns.exception.BadException;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.function.Supplier;

/**
 * 断言类
 *
 * @author luwc
 * @title Assert
 * @description
 * @time 2023/5/10 17:15:02
 * @version: 1.0
 */
public class Assert {
    /**
     * 断言对象不为空
     * 如果对象obj为空，则抛出异常
     *
     * @param obj
     * @param resultCode
     * @return void
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/5/10 17:20:57
     */
    public static void notNull(Object obj, ResultCode resultCode) {
        if (obj == null) {
            throw new BadException(resultCode);
        }
    }


    /**
     * 断言对象为空
     * * 如果对象obj不为空，则抛出异常
     *
     * @param object
     * @param resultCode
     * @return void
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/5/10 17:20:49
     */
    public static void isNull(Object object, ResultCode resultCode) {
        if (object != null) {
            throw new BadException(resultCode);
        }
    }

    /**
     * 断言表达式为真
     * 如果不为真，则抛出异常
     *
     * @param expression
     * @param resultCode
     * @return void
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/5/10 17:21:22
     */
    public static void isTrue(boolean expression, ResultCode resultCode) {
        if (!expression) {
            throw new BadException(resultCode);
        }
    }

    /**
     * 断言表达式为假
     * 如果不为真，则抛出异常
     *
     * @param expression
     * @param resultCode
     * @return void
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/5/10 17:21:45
     */
    public static void isFalse(boolean expression, ResultCode resultCode) {
        if (expression) {
            throw new BadException(resultCode);
        }
    }

    /**
     * 断言两个对象不相等
     * 如果相等，则抛出异常
     *
     * @param m1
     * @param m2
     * @param resultCode
     * @return void
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/5/10 17:22:34
     */
    public static void notEquals(Object m1, Object m2, ResultCode resultCode) {
        if (m1.equals(m2)) {
            throw new BadException(resultCode);
        }
    }

    /**
     * 断言两个对象相等
     * 如果不相等，则抛出异常
     *
     * @param m1
     * @param m2
     * @param resultCode
     * @return void
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/5/10 17:22:41
     */
    public static void equals(Object m1, Object m2, ResultCode resultCode) {
        if (!m1.equals(m2)) {
            throw new BadException(resultCode);
        }
    }

    /**
     * 断言参数不为空
     * 如果为空，则抛出异常
     *
     * @param s
     * @param resultCode
     * @return void
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/5/10 17:23:22
     */
    public static void notEmpty(String s, ResultCode resultCode) {
        if (StringUtils.isEmpty(s)) {
            throw new BadException(resultCode);
        }
    }

    /**
     * 断言参数不为空
     * 如果为空，则抛出异常
     *
     * @param list
     * @param resultCode
     * @return void
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/5/10 17:23:12
     */
    public static void notEmpty(List<Long> list, ResultCode resultCode) {
        if (list == null || list.isEmpty()) {
            throw new BadException(resultCode);
        }
    }

    /**
     * 定制
     *
     * @param supplier
     * @param resultCode
     * @return void
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/5/10 17:23:43
     */
    public static void custom(Supplier<Boolean> supplier, ResultCode resultCode) {
        if (supplier.get()) {
            throw new BadException(resultCode);
        }
    }
}
